<?php

class GroupController extends Zend_Controller_Action 
{
    protected $group;
    protected $marker;
    protected $_form;
    public function indexAction()
    {
        $group = $this->getGroup();
        $this->view->url = $this->view->url(array('controller' => 'group', 'action' => 'markers'),'default', true).'/?id=';        
        $this->view->groups = $group->fetchEntries();
        $this->view->bodyScript = $this->getLoadScript($this->view->groups[0]['id']);        
    }
    
    public function viewAction()
    {
        $request = $this->getRequest();
        $group_id = $request->getParam('id');        
        $group = $this->getGroup();        
        $this->view->group = $group->fetchEntry($group_id);
        
        //$src = $this->mapHeadSource();        
        //$this->view->headScript()->appendFile($src);
        $this->view->url = $this->view->url(array('controller' => 'group', 'action' => 'markers'),'default', true).'/?id=';
        //$src = $this->mapScript($url);        
        //$this->view->headScript()->appendScript($src);
        $this->view->bodyScript = $this->getLoadScript($group_id);        
    }
     
    public function lastAction(){
        $group = $this->getGroup();
        $res = $group->getLast(5);
        return $res;
    }
    
    public function itemsAction(){        
        $request = $this->getRequest();
        $start = $request->getParam('start');
        $limit = $request->getParam('limit');
        $query = $request->getParam('query');
        $callback = $request->getParam('callback');
        
        $group = $this->getGroup();
        $count = $group->searchGroupsCount($query);
        $data = $group->searchGroups($query, $start, $limit);
                
        $final = array();
        $final['totalCount'] =  strval($count);
        $final['results'] = $data;        
        header('Content-Type', 'application/x-json');
        echo $callback.'('.Zend_Json::encode($final).');';
        die();        
    }
   
    protected function getGroup()
    {
        if (null === $this->group) {
            require_once APPLICATION_PATH . '/models/Group.php';
            $this->group = new Model_Group();
        }
        return $this->group;
    }
     protected function getMarker()
    {
        if (null === $this->marker) {
            require_once APPLICATION_PATH . '/models/Marker.php';
            $this->marker = new Model_Marker();
        }
        return $this->marker;
    }
    
    public function getLoadScript($id){
        $script = 'onload="load('.$id.')" onunload="GUnload()"';
        return $script;
    }
    public function mapHeadSource(){
        $script = 'http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAA0kL-OiF-SdKHJLK-EK6aJBTPAo1rtDnXrzBooSDs6G9xpoWBNBTq4LchskrfSp193jk0bo7P4gcYzg';    
        return $script;
    }
    public function simpleMap(){
        $script = 'function load() {
                        if (GBrowserIsCompatible()) {
                        var map = new GMap2(document.getElementById("map"));
                        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
                        }
                    }';
        return $script;
    }
   
    public function markersAction(){
        $request = $this->getRequest();
        $group_id = $request->getParam('id');   
        
        $dom = new DOMDocument("1.0", "utf-8");
        $node = $dom->createElement("markers");
        $parnode = $dom->appendChild($node); 
        
        $marker = $this->getMarker();
        $rows = $marker->fetchByGroupId($group_id);
        
        header("Content-type: text/xml"); 
        
        foreach($rows as $m){
            $node = $dom->createElement("marker");  
            $newnode = $parnode->appendChild($node);   
            $newnode->setAttribute("name",$m['name']);
            $newnode->setAttribute("address", $m['name']);  
            $newnode->setAttribute("lat", $m['latitude']);  
            $newnode->setAttribute("lng", $m['longitude']);  
            $newnode->setAttribute("type", 'restaurant');
        } 
        echo $dom->saveXML();        
        die();
    }
    
    private function getSearchForm(){
        require_once APPLICATION_PATH . '/forms/Search.php';
        $form = new Form_Search();
        $form->setAction($this->_helper->url('search'));
        return $form;   
    }  
     
}
